/**
 * Copyright (c) 2018, dreamkaylee@foxmail.com All Rights Reserved.
 */
  
package configuration;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

/**
 * 缓存配置
 * @author limk
 * @date 2018年2月11日 下午3:07:12
 * @version 1.0
 */
@Configuration
@EnableCaching
public class CacheConfig {
	
	private static final Logger LOGGER = LogManager.getLogger(CacheConfig.class);
	
	/**
	 * Spring提供的基于的Ehcache实现的缓存管理器
	 * @return
	 */
	@Bean
	public EhCacheManagerFactoryBean cacheManagerFactoryBean() {
		EhCacheManagerFactoryBean bean = new EhCacheManagerFactoryBean();
		bean.setConfigLocation(new ClassPathResource("ehcache.xml"));
		return bean;
	}
	
	@Bean
	public CacheManager springCacheManager() {
		LOGGER.info("----------Spring CacheManager----------");
		EhCacheCacheManager manager = new EhCacheCacheManager();
		manager.setCacheManager(cacheManagerFactoryBean().getObject());
		manager.setTransactionAware(true);
		return manager;
	}
	
}
  